<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/4/10
 * Time: 11:30
 */
namespace app\controller;

use app\domain\StickPay;
use app\domain\StickUser;
use Endroid\QrCode\Exceptions\ImageFunctionFailedException;
use rap\config\Config;
use rap\db\Where;
use rap\web\Request;
use rap\web\Response;
use app\domain\user;
use app\domain\StickUser as domainSU;
use app\domain\StickPic;
use app\server\request as modelRequest;

class StickController{

    /**
     * 首页
     * @param Request $request
     * @param Response $response
     * @return string
     */
    public function index (Request $request, Response $response) {

        if ($request->method() == "GET"){

            $agent = user::getUserToken($_SERVER['HTTP_USER_AGENT']);

            $agent['token'] = '9fd1dd6b83f623c57e7108c835a841b1';

            if (!$agent['token']){
                $response->assign('alert', '请先登录');
                return CommonController::false(404, '请先登录',[]);
            }
            $data = CommonController::getUserInfo($agent['token']);
            $where = [
                'user_id' => $data['data']['user_id'],
                'isdelete' => 0
            ];

            $userInfo = domainSU::getStickUserInfo($where, "*");
//            $gold = user::getUserGoldCount($res['user_id']);
            if (!$userInfo) {
                $data['data']['regist_time'] = date('Y-m-d H:i:s',  $data['data']['regist_time']);
                domainSU::insertUserInfo($data['data']);
            }

            $userInfo = domainSU::getStickUserInfo($where, "*");

            $_SESSION[$agent['token']] = $userInfo;
        }
        $response->assign('account', $userInfo['account_currency']);

        return 'Stick/index';
    }

    /**
     * 车贴申请
     * @param Request $request
     * @param Response $response
     * @return string
     */
    public function apply (Request $request, Response $response) {

        $title = "申请车贴";

        $response->assign("title", $title);

//        $conf = Config::getFileConfig();
//
//        $where = [
//            'secret' => $conf['app_info']['secret'],
//        ];
//        $data = modelRequest::getMethod($conf['payUrl']['sum_url'], $where);
//
//        print_r($data);
//        die();
        return "Stick/apply";
    }

    public function upload(Request $request, Response $response) {
        $title = "车贴上传";

        $response->assign("title", $title);
        return "Stick/upload";
    }

    /**
     * 申请记录
     * @param Request $request
     * @param Response $response
     * @return string
     */
    public function record(Request $request, Response $response){

        $title = "上传记录";

//        $where = [
//            'user_id' => $_SESSION,
//            'isdelete' => 0,
//        ];
//        $field = '*';
//        $data = StickPic::getStickPicList($where, $field, 1);
//        $response->assign('title', $title);
//        $response->assign('list', $data);

        return "Stick/record";
    }

    /**
     * 排行榜
     * @param Request $request
     * @param Response $response
     * @return string
     */
    public function rank (Request $request, Response $response) {

        $title = "排行榜";

        $response->assign('title', $title);
        $response->assign('account', 90000);
        return "Stick/rank";
    }

    public function history(Request $request, Response $response) {
        $title = "支付记录";

     $response->assign("title", $title);
    return "Stick/history";
    }

    public function cash(Request $request, Response $response) {
        if ($request->method() == "GET") {
        }
        $title = "提现";

        $where = [
            'user_id' => 3105,
            'isdelete' => 0
        ];
        $field = "account_currency";
        $cash = StickUser::getStickUserInfo($where, $field);
        $response->assign("title", $title);
        $response->assign("cash", $cash);

        return "Stick/cash";
    }

    public function addStickInfo() {

        return CommonController::success('提交成功', []);
    }
/*********************************************************API********************************************************/
    /**
     * 提交车贴信息
     */
    public function uploadStickPic (Request $request) {

        if ($request->method() == "POST") {
            $common = new OcrController();

            $res = $common->getUserCarNumberInfo();

            $data = json_decode($res);
            $carInfo = $data -> cardsinfo;
            $carInfo = $carInfo[0]->items;
            $carNumber = $carInfo[0]->content;

            $owner_phone = $request->param('owner_phone');
            $intro = $request->param('intro');
            $pic = $request->param('pic');
            $car_number = $request->param('car_number');

            if (!$owner_phone) {
                return CommonController::false(401, '手机号不能为空', []);
            }

            $res = CommonController::checkUserPhone($owner_phone);
            if ($res == 0) {
                return CommonController::false(402, '手机号格式不正确',[]);
            }

            if (!$car_number){
                return CommonController::false(401, '车牌号码不能为空', []);
            }

            if ($car_number != $carNumber){
                return CommonController::false(402, '填写号码与车牌号码不一致',[]);
            }
            $where = [
              'car_number' => $car_number,
                'is_del' => 0
            ];
            $res = StickPic::getStickCarInfo( $where, $field = "*");
            if ($res) {
                return CommonController::false(403, "该车辆已经张贴过车贴", []);
            }
            $data = [
                'owner_phone' => $owner_phone,
                'intro' => $intro,
                'pic_url' => $pic,
                'car_number' => $car_number
            ];

            $res = StickPic::addStickPicInfo($data);


            if ($res) {
                return CommonController::success('上传成功', []);
            } else {
                return CommonController::false(404, '上传失败', []);
            }

        }
    }

    public function getStickPicList(Request $request) {

        if ($request->method() == "POST") {
            $page = $request->param('page', 1);
            $token = $request->param('token', '');

            if (!$token) return CommonController::false(401, '请重新登录', []);

            $where = [
                'user_id' => $_SESSION[$token]['user_id'],
                'isdelete' => 0,
            ];
            $field = '*';
            $data = StickPic::getStickPicList($where, $field, $page);
            if ($data) {
                return CommonController::success('获取数据成功', $data);
            } else {
                return CommonController::false(404, '暂无查询数据', []);
            }
        }
    }

    public function applyCash (Request $request) {

        if ($request->method() == "POST") {

            $amount = $request->param('amount');
            $token = $request->param('token', '9fd1dd6b83f623c57e7108c835a841b1');

            $data = [
                'user_id' => $_SESSION[$token]['user_id'],
                'amount' => $amount,
                'create_time' => date('Y-m-d H:i;s', time())
            ];

            $sql = "UPDATE `mp_stick_user` SET `account_currency`=account_currency- $amount WHERE `user_id` =".$_SESSION[$token]['user_id']." AND `isdelete` = 0";

            StickPay::excelSql($sql);

            $res = StickPay::addUserCashInfo($data);

            if ($res) {
                return CommonController::success('申请成功，请等待管理员审核', []);
            } else {
                return CommonController::false(404, '申请失败', []);
            }
        }
    }


    public function getUserCoinList(Request $request) {

        if ($request->method() == "POST") {

            $page = $request->param('page', 0);

            $where = [
                'isdelete' => 0
            ];
            $field = "id, name, head, account_currency";

            $order = ['account_currency'=>'desc','create_time'=>'desc'];
            $list = StickUser::getUserList($where, $field, $page, $order);

            if ($list) {
                return CommonController::success('获取数据成功', $list);
            } else {
                return CommonController::false(404, '暂无数据', []);
            }
        }
    }

    public function getUserPayList (Request $request) {
        if ($request->method() == "POST") {
            $page = $request->param('page', 0);
            $limit = $request->param('size', 10);
            $token = $request->param('token');

            $where = [
                'sp.user_id' => $_SESSION[$token]['user_id'],
//                'sp.user_id' =>3105,
                'sp.status' => 1,
                'sp.isdelete' =>0
            ];
            $field = 'su.name, sp.id, sp.amount, sp.pay_type, sp.create_time';

            $data = StickPay::getUserPayInfo($where, $field, $limit, $page);
            if ($data) {
                return CommonController::success('获取数据成功', $data);
            } else {
                return CommonController::false(404,'暂无数据', $data);
            }
        }

    }
}
